package com.chinatelecom.telecollege.sellerrewarding.controller;

import com.chinatelecom.telecollege.sellerrewarding.Entity.seller;
import com.chinatelecom.telecollege.sellerrewarding.service.sellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.chinatelecom.telecollege.sellerrewarding.token.DefaultTokenManager ;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by lenovo on 2017/6/21.
 */
@RestController
public class sellerController {
    @Autowired
    private sellerService sellerService;
    private DefaultTokenManager  tokenManager = new DefaultTokenManager ();

    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public String login(@RequestParam String phone,@RequestParam String password) {
        seller seller = sellerService.find(phone);
        if(null!=seller && seller.getPassword().equals(password))
            return "{status:'Success',name:'"+seller.getName()+
                    "',isadmin:"+seller.isIsadmin()+
                    ",isassistant:"+seller.isIsassistant()+
                    ",token:'"+tokenManager.createToken(phone)+"'}";
        else return "Failed";
    }

    @RequestMapping(value = "/seller")
    public seller queryseller(@RequestParam String phone, @CookieValue("JSESSIONID") String sessionId) {
        if(tokenManager.checkToken(sessionId)) return sellerService.find(phone);
        else return new seller();
    }

    @RequestMapping(value = "/sellerall")
    public List<seller> querysellerall(@CookieValue("JSESSIONID") String sessionId) {
        if(tokenManager.checkToken(sessionId)) {
            List<seller> sellers = sellerService.findAll();
            for(seller seller :sellers){
                seller.setPassword("");
            }
            return sellers;
        }else return new ArrayList<seller>();
    }

    @RequestMapping(value = "/selleradd")
    public String addseller(@RequestParam String phone,
                          @RequestParam String name,
                          @RequestParam String password,
                          @RequestParam boolean isadmin,
                          @RequestParam boolean isassistant,
                          @CookieValue("JSESSIONID") String sessionId) {
        System.out.println(name+" : "+isadmin);
        if(tokenManager.checkToken(sessionId)) {
            seller seller = sellerService.find( tokenManager.getuser(sessionId) );
            if (seller.isIsadmin()){
                sellerService.save(phone, name, password, isadmin, isassistant);
                return "Success";
            }return "notadmin";
        }return "notlogin";
    }

    @RequestMapping(value = "/sellerdel")
    public String delseller(@RequestParam String phone,
                            @CookieValue("JSESSIONID") String sessionId) {
        if(tokenManager.checkToken(sessionId)) {
            seller seller = sellerService.find( tokenManager.getuser(sessionId) );
            if (seller.isIsadmin()){
                sellerService.delete(sellerService.find(phone));
                return "Success";
            }return "notadmin";
        }else return "notlogin";
    }

    @RequestMapping(value = "/sellermod")
    public String modseller(@RequestParam String phone,
                          @RequestParam boolean isadmin,
                          @RequestParam boolean isassistant,
                          @CookieValue("JSESSIONID") String sessionId) {

        if(tokenManager.checkToken(sessionId)) {
            sellerService.mod(phone, isadmin, isassistant);
            return "Success";
        }else return "Failed";
    }
}
